<template>
  <div>
    <Form :model="formData" @submit.native.prevent="save" :label-width="80">
      <FormItem label="角色名称">
        <Input v-model="formData.title" />
      </FormItem>
      <FormItem label="选择权限">
        <Tree :data="authList" ref="authTree" check-directly show-checkbox></Tree>
      </FormItem>

      <FormItem>
        <Button type="primary" html-type="submit" :loading="formLoading">保存</Button>
      </FormItem>
    </Form>
  </div>
</template>
<script>
import addPage from "../../mixins/addPage";
export default {
  data() {
    return {
      formData: { title: "", rules: "" },
      formLoading: false,
      authList: []
    };
  },
  created() {
    this.$axios.get("/admin/auth_rule/getAll").then(res => {
      if (res.code == 1) {
        this.authList = res.data;
      }
    });
  },
  mixins: [addPage],
  methods: {
    save() {
      const selected = this.$refs.authTree.getCheckedAndIndeterminateNodes();
      let ids = [];
      selected.forEach(item => {
        ids.push(item.id);
      });
      this.formData.rules = ids;
      this.formLoading = true;
      this.$axios.post("/admin/auth_group/add", this.formData).then(res => {
        this.formLoading = false;
        if (res.code == 1) {
          this.$Message.success("操作成功");
          this.closeTag(this.$route);
        } else {
          this.$Message.error(res.msg);
        }
      });
    }
  }
};
</script>
